package com.runjian.rundo.iot.agent.business.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.runjian.rundo.iot.agent.common.entity.AgentDeviceDTO;
import com.runjian.rundo.iot.agent.common.manager.DeviceManager;

import cn.hutool.core.map.multi.RowKeyTable;
import cn.hutool.core.map.multi.Table;
import cn.hutool.core.util.ObjectUtil;

/**
 * IoT设备服务接口
 *
 * @author LiGang
 * @date 2023/4/13 22:02
 */
public class BaseDeviceManagerImpl<T extends AgentDeviceDTO> implements DeviceManager<T> {

    private Map<String, T> deviceMap = new HashMap<>();

    /**
     * 设备列表。现在一个设备ID会对应多个配置，因为东方电子的点号不是固定的，
     */
    private Map<String, List<T>> deviceListMap = new HashMap<>();
    /**
     * 设备缓存列表
     */
    private static final Table<String, String, List<AgentDeviceDTO>> gatewayDeviceMap = new RowKeyTable();

    @Override
    public <T extends AgentDeviceDTO> List<T> getDeviceByExternalId(String gatewayId, String externalId) {
        List<AgentDeviceDTO> agentDeviceDTOS = gatewayDeviceMap.get(gatewayId, externalId);
        if (CollectionUtil.isEmpty(agentDeviceDTOS)){
            return new ArrayList<>();
        }
        return agentDeviceDTOS.stream().map(e -> (T) e).collect(Collectors.toList());
    }

    @Override
    public T getDeviceById(String deviceId) {
        return deviceMap.get(deviceId);
    }

    @Override
    public List<T> getDeviceListById(String deviceId) {
        return deviceListMap.get(deviceId);
    }

    @Override
    public  List<AgentDeviceDTO> getDeviceMap(String gatewayId,String deviceType) {
        List<AgentDeviceDTO> list=new ArrayList<>();
        Map<String, List<AgentDeviceDTO>> listMap=gatewayDeviceMap.getRow(gatewayId);
        if(ObjectUtil.isNotEmpty(listMap)){
            listMap.forEach((k,v)->{
                list.addAll(v.stream().filter(e -> e.getDeviceType().equals(deviceType)).collect(Collectors.toList()));
              }
            );
        }
        return list;
    }

    @Override
    public void addLocalDeviceList(List<T> devices) {
        if (ObjectUtil.isEmpty(devices)) {
            return;
        }
        devices.forEach(device -> {
            deviceMap.put(device.getDeviceId(), device);
            //  这里不能用  device.getDeviceId() 当做key ，因为 deviceId 重复，需要增加
            List<T> ts = deviceListMap.get(device.getDeviceId());
            if (CollUtil.isEmpty(ts)) {
                ts = CollUtil.newArrayList();
                deviceListMap.put(device.getDeviceId(), ts);
            }
            ts.add(device);
            List<AgentDeviceDTO> deviceLists=gatewayDeviceMap.get(device.getGatewayId(), device.getExternalId());
            if(null==deviceLists){
                deviceLists= CollUtil.newArrayList();
                gatewayDeviceMap.put(device.getGatewayId(), device.getExternalId(),deviceLists);
            }
            deviceLists.add(device);
        });
    }

}
